package com.som.service.excel.data3.state;

import java.util.ArrayList;
import java.util.List;

import com.som.bean.data3.IpAddressInfo;
import com.som.bean.data3.IpMaskInfo;
import com.som.bean.data3.IpSectionInfo;
import com.som.console.CfgEnum;
import com.som.util.IpAddressUtil;

/**
 * 将IP掩码的形式数据转换为其他格式的数据
 * 
 * @author liujun
 * @date 2016年4月12日
 * @verion 0.0.1
 */
public class ParseIpMaskStateImp implements ParseStateInf<List<IpMaskInfo>>
{

	/**
	 * 上下文对象
	 */
	private StateContext context;

	public ParseIpMaskStateImp(StateContext context)
	{
		this.context = context;
	}

	/**
	 * 将掩码转换为IP地址信息
	 */
	public List<IpAddressInfo> parseIp(List<IpMaskInfo> param)
	{
		if (null != param && !param.isEmpty())
		{
			List<IpAddressInfo> result = new ArrayList<IpAddressInfo>();

			IpAddressInfo ipaddr = null;

			for (IpMaskInfo ipMaskInfo : param)
			{
				ipMaskInfo.setIpMask(ipMaskInfo.getIpMask().trim());
				int spitIndex = ipMaskInfo.getIpMask().lastIndexOf("/");
				if (-1 != spitIndex)
				{
					// 1取得IP与掩码
					String ip = ipMaskInfo.getIpMask().substring(0, spitIndex);
					int mask = Integer.parseInt(ipMaskInfo.getIpMask().substring(spitIndex + 1));

					// 得到尾地址
					int maskValue = (int) Math.pow(2, 32 - mask);
					int startValueIP = Integer.parseInt(ip.substring(ip.lastIndexOf(".") + 1));
					int endValueIP = startValueIP + maskValue - 1;
					String endIp = ip.substring(0, ip.lastIndexOf(".") + 1) + endValueIP;

					List<String> ipsecion = IpAddressUtil.getIpListBySecation(ip, endIp);

					if (null != ipsecion && !ipsecion.isEmpty())
					{
						for (String ipaddrs : ipsecion)
						{
							ipaddr = new IpAddressInfo();
							ipaddr.setIpaddress(ipaddrs);
							ipaddr.setMsginfo(ipMaskInfo.getMsgInfo());
							result.add(ipaddr);
						}
					}
				} else
				{
					ipaddr = new IpAddressInfo();
					ipaddr.setIpaddress(ipMaskInfo.getIpMask());
					ipaddr.setMsginfo(ipMaskInfo.getMsgInfo());
					result.add(ipaddr);
				}
			}

			return result;
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public List<IpSectionInfo> parseIpSection(List<IpMaskInfo> param)
	{
		// 先将掩码转换成IP
		List<IpAddressInfo> maskIPList = this.parseIp(param);

		// 设置当前的处理为ip段的处理
		this.context.setCurrParse(CfgEnum.SYSTEM_FORMAT_TYPE_IPSECTION.getCfgMsg());
		// 再将IP转换成IP段
		List<IpSectionInfo> list = this.context.getCurrParse().parseIpSection(maskIPList);

		return list;
	}

	public List<IpMaskInfo> parseIpMask(List<IpMaskInfo> param)
	{
		return param;
	}

	public static void main(String[] args)
	{
		StateContext context = new StateContext();
		ParseIpMaskStateImp mask = new ParseIpMaskStateImp(context);

		List<IpMaskInfo> param = new ArrayList<IpMaskInfo>();

		IpMaskInfo ipmask = new IpMaskInfo();
		ipmask.setIpMask("192.168.1.1/30");
		ipmask.setMsgInfo("信息1");
		param.add(ipmask);

		// IpMaskInfo ipmask2 = new IpMaskInfo();
		// ipmask2.setIpMask("192.168.2.1/29");
		// ipmask2.setMsgInfo("信息1");
		// param.add(ipmask2);

		List<IpAddressInfo> list = mask.parseIp(param);

		System.out.println(list);

	}

}
